package com.chen.authority.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.authority.common.util.FileUtil;
import com.chen.authority.common.util.SaTokenUtil;
import com.chen.authority.mapper.FileMapper;
import com.chen.authority.model.po.FilePo;
import com.chen.authority.service.FileService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 文件Service业务层处理
 *
 * @author chengy
 * @date 2022-11-19
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FilePo> implements FileService{

    @Override
    public boolean saveUploadFileInfo(String originalFilename, long size, String url, String remark) {
        FilePo file = new FilePo();
        String[] path = url.split(StrPool.SLASH);
        String filename = path[path.length - 1];
        file.setFilename(filename);
        file.setOriginalName(originalFilename);
        file.setFileSuffix(FileNameUtil.extName(filename));
        file.setFileSize(FileUtil.convertFileSize(size));
        file.setUrl(url);
        file.setUploadBy(SaTokenUtil.getLoginUsername());
        file.setUploadTime(new Date());
        file.setRemark(ObjectUtil.isNull(remark) ? StrUtil.EMPTY : remark);
        return this.save(file);
    }

}